package com.ycrlkj.android.modulespublic.common.widget.iconfont;

import android.util.Log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * iconfont文件下载, iconfont.ttf对应iconfont.json文件自动转android string.xml文件 字符串列表
 */
public final class IconfontUtil {

    public static void main(String[] args) {
        try {
            iconfontToResource("D:/iconfont.json", "D:/iconfont.text");
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("ming", " 写入文件失败 " );
        }
    }

    public static void iconfontToResource(String jsonPath, String sqlPath) throws Exception {

        //String data = readFile("D:/iconfont.json");
        String data = readFile(jsonPath);

        JSONObject jsonObj = JSONObject.parseObject(data);

        JSONArray arr = jsonObj.getJSONArray("glyphs");

        for (Object o : arr) {
            JSONObject item = (JSONObject) o;

            //获取类名
            String font_class = item.getString("font_class");
            String className = font_class.replaceAll("-", "_");//原iconfont类名中有连词符 - , 在strings.xml资源文件中连词符 - 是违法字符, 全部替换成下划线

            //获取unicode
            String unicode = item.getString("unicode");
            String itemStr = "<string name=\"" + className + "\">&#x" + unicode + ";</string> \n";

            //writeFile(itemSql, "D:/iconfont.text");
            writeFile(itemStr, sqlPath);
        }
    }

    /**
     * 写入文件
     */
    public static void writeFile(String content, String path) throws IOException {
        File file;
        file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fileWriter = new FileWriter(file, true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(content);
        bufferedWriter.close();
    }

    public static String readFile(String path) {
        File file = new File(path);
        BufferedReader reader = null;
        StringBuilder lastStr = new StringBuilder();
        try {
            // System.out.println("以行为单位读取文件内容，一次读一行");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                System.out.println("line " + line + ": " + tempString);
                lastStr.append(tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    System.out.println("关闭文件发生错误, ");
                    e1.printStackTrace();
                }
            }
        }
        return lastStr.toString();
    }
}
